package com.ltxtiyu.video.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.ltxtiyu.video.entity.Manager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class AjaxIntegerceptor implements HandlerInterceptor {
    public static final Logger log = LoggerFactory.getLogger(AjaxIntegerceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Manager loginUser = (Manager) request.getSession().getAttribute("loginUser");

        if(loginUser == null){
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            PrintWriter out = null ;
            try{

                JSONObject res = new JSONObject();
                res.put("code",-1);
                res.put("msg","登陆超时");
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            catch (Exception e){
                e.printStackTrace();
                response.sendError(500);
                return false;
            }
        }else{

            return true;

        }

    }
}
